package br.ufc.si.managedBean;

import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.persistence.PersistenceException;
import javax.servlet.http.HttpServletRequest;

import modelo.Propaganda;
import dao.PropagandaDao;

@ManagedBean(name = "cadastroPropagandaBean")
public class CadastroPropagandaBean {
	private Propaganda propaganda;

	public CadastroPropagandaBean(){
		this.propaganda = new Propaganda();
	}

	private String msg;
	
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Propaganda getPropaganda() {
		return propaganda;
	}

	public void setPropaganda(Propaganda propaganda) {
		this.propaganda = propaganda;
	}

	public String paginaPropaganda() {

		return "/admin/propaganda/cadastrar_propaganda";
	}

	public String cadastrar() {
		PropagandaDao propagandaDao = new PropagandaDao();
		try {
			propagandaDao.save(propaganda);
			propaganda = new Propaganda();
			msg = "Propaganda cadastrada com sucesso";
			return "/admin/index_admin?faces-redirect=true";

		} catch (PersistenceException ex) {
			ex.printStackTrace();
			msg = "Propaganda não cadastrada";

			return "/admin/index_admin?faces-redirect=true";
		}
	}

	public String cancelar() {
		return "/admin/index_admin";
	}

	public String atualizar() throws ClassNotFoundException {

		PropagandaDao propagandaDao = new PropagandaDao();
		try {
			propagandaDao.update(propaganda);
			msg = "Propaganda atualizado";

			return "/admin/index_admin";

		} catch (PersistenceException ex) {
			ex.printStackTrace();
			msg = "Erro ao atualizar propaganda";
			return "/admin/index_admin";
		}

	}

	public String visualizar() {

		PropagandaDao propagandaDao = new PropagandaDao();
		HttpServletRequest request = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest();

		try {
			propaganda = propagandaDao.find(Integer.parseInt(request
					.getParameter("id")));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return "/admin/propaganda/visualizar_propaganda";
	}

	public boolean getMostraTabela() {

		PropagandaDao propagandaDao = new PropagandaDao();
		return propagandaDao.getLista().size() > 0;
	}

	public String editar() {

		PropagandaDao propagandaDao = new PropagandaDao();
		HttpServletRequest request = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest();

		try {
			propaganda = propagandaDao.find(Integer.parseInt(request
					.getParameter("id")));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}

		return "/admin/propaganda/editar_propaganda";
	}

	public String excluir() {

		PropagandaDao propagandaDao = new PropagandaDao();
		HttpServletRequest request = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest();

		Propaganda propaganda = propagandaDao.find(Integer.parseInt(request.getParameter("id")));
		propagandaDao.delete(propaganda);

		return "/admin/index_admin?faces-redirect=true";
	}

	public PropagandaDao getPropagandaDao(){
		return new PropagandaDao();
	}
	
}
